class Solution {
public:
    int Nb[26][26];
    int CountLife(int i, int j, vector<vector<int>>& board) {
        int col = board[0].size();
        int row = board.size();
        int cnt = 0;

        // 检查周围八个格子
        int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
        int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
        for (int k = 0; k < 8; k++) {
            int newX = i + dx[k];
            int newY = j + dy[k];
            // 检查越界情况
            if (newX >= 0 && newX < row && newY >= 0 && newY < col) {
                cnt += board[newX][newY];
            }
        }

        return cnt;
    }
   void gameOfLife(vector<vector<int>>& board) {
    // 声明游戏有几种情况 首先 如果是死细胞
    // 则遍历周围八个看看有没有三个活细胞 如果有对矩阵对应位置设置成-1
    // 表示要变 如果是活细胞 就看看周围有几个细胞 少于两个或者大于三个都死亡
    // 等于两个和三个存存活

    int row = board.size();
    int col = board[0].size();

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; ++j) {
            int cnt = CountLife(i, j, board);
            if (board[i][j] == 0) {
                if (cnt == 3)
                    Nb[i][j] = -1;
            }
            if (board[i][j] == 1) {
                if (cnt < 2 || cnt > 3)
                    Nb[i][j] = -1;
            }
        }
    }

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; ++j) {
            if (board[i][j] == 0 && Nb[i][j] == -1) {
                    board[i][j] = 1;
            }
            else if (board[i][j] == 1 && Nb[i][j] == -1) {
                    board[i][j] = 0;
            }
        }
    }
}
};
